﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using pcsse.App_Code.entity;

namespace pcsse.activity
{
    public partial class updateactivity : System.Web.UI.Page
    {
        public const String UpAttach = "UpAttach/";

        protected void Page_Load( object sender, EventArgs e )
        {
            errLb.Text = "";
            if ( Session["ID"] == null )
            {
                Response.Redirect( "~/default.aspx" );
            }
        }

        protected void mainGv_RowDataBound( object sender, GridViewRowEventArgs e )
        {
            if ( e.Row.RowType == DataControlRowType.DataRow )
            {
                Label lb = (Label)e.Row.FindControl( "stateLb" );
                int state = Convert.ToInt32( lb.Text );
                switch ( state )
                {
                    case 0:
                        lb.Text = "未审批";
                        break;
                    case 1:
                        lb.Text = "驳回";
                        break;
                    case 2:
                        lb.Text = "通过";
                        break;
                }
            }
        }

        protected void activityDv_DataBound( object sender, EventArgs e )
        {
            Label lb = (Label)activityDv.FindControl( "stateDvLb" );
            if ( lb != null )
            {
                int state = Convert.ToInt32( lb.Text );
                switch ( state )
                {
                    case 0:
                        lb.Text = "未审批";
                        break;
                    case 1:
                        lb.Text = "驳回";
                        break;
                    case 2:
                        lb.Text = "通过";
                        break;
                }
            }
        }

        protected void activityDv_ItemUpdated( object sender, DetailsViewUpdatedEventArgs e )
        {
            if ( e.Exception != null )
            {
                Exception ie = e.Exception.InnerException;
                if ( ie is ArgumentException )
                {
                    string argu = ( (ArgumentException)ie ).ParamName;
                    errLb.Text = "参数[";
                    errLb.Text += argu;
                    errLb.Text += "]不合法!";
                }
                else if ( ie is NoNullAllowedException )
                {
                    errLb.Text = "一个或多个字段为空!";
                }
                else if ( ie is System.Data.Common.DbException )
                {
                    errLb.Text = "数据库异常，请稍后再试";
                }
                else
                {
                    errLb.Text = "未知错误，请稍后再试";
                }
                //e.ExceptionHandled = true;
                e.KeepInEditMode = true;
            }
            else
            {
                mainGv.DataSourceID = "mGvOds";
                mainGv.SelectedIndex = -1;
            }
        }

        protected void upLoadBt_Click( object sender, EventArgs e )
        {
            try
            {
                FileUpload attachFu = (FileUpload)activityDv.FindControl( "attachFu" );
                if ( attachFu.HasFile )
                {
                    string filename = System.DateTime.Now.ToFileTimeUtc() + attachFu.FileName;
                    attachFu.SaveAs( Server.MapPath( UpAttach ) + filename );
                    TextBox tb = ( (TextBox)activityDv.FindControl( "upFileTb" ) );
                    tb.Text = filename;
                }
                else
                {
                    errLb.Text = "请选择文件";
                }
            }
            catch ( System.Exception ex )
            {
                errLb.Text = ex.ToString();
            }
        }

        protected void activityDv_ItemUpdating( object sender, DetailsViewUpdateEventArgs e )
        {
            try
            {
                errLb.Text = "";
            }
            catch ( System.Exception ex )
            {

            }
        }

        protected void mGvOds_Selecting( object sender, ObjectDataSourceSelectingEventArgs e )
        {
            if ( Session["ID"] == null )
            {
                Response.Redirect( "~/default.aspx" );
            }
            else
            {
                e.InputParameters.RemoveAt( 0 );
                e.InputParameters.Add( "NUMBER", ( new person_info() ).GetNumberByID( Convert.ToInt32( Session["ID"] ) ) );
            }
        }
    }
}
